package cn.itcast.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
@Slf4j
public class AddressController {

    @Reference(version = "1.0.0")
    private AddressService addressService;

    /**
     * 查询所有分院信息
     *
     * @return
     */
    @GetMapping("findAll")
    public Result findAll() {
        try {
            List<Address> addresses = addressService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, addresses);
        } catch (Exception e) {
            log.error("查询所有失败，失败原因：{}",e);
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    /**
     * 根据id查询单个院区信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Address address = addressService.findById(id);
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, address);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }


    /**
     *
     *因为编辑与新增共用新增表，所以要进行判断，根据Id查询address数据 若有则编辑，反之则新建
     * @param address
     * @return
     */
    @PostMapping("submit")
    public Result submitAddress(@RequestBody Address address) {

        log.info("submit请求进来了");

        Address result = addressService.findById(address.getId());

        if (ObjectUtil.isNotEmpty(result)) {
            Result result1 = this.updateAddress(address);

            return result1;
        } else {
            Result result1 = this.addAddress(address);

            return result1;
        }
    }

    /**
     * 新增Address
     *
     * @param address
     * @return
     */
    @PostMapping
    public Result addAddress(Address address) {

        log.info("新增请求进来了");

        try {
            addressService.addAddress(address);

            return new Result(true,MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            log.error("新增失败，失败原因：{}",e);
            return new Result(false,MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    /**
     * 编辑Address信息
     *
     * @param address
     * @return
     */
    @PutMapping
    public Result updateAddress(Address address) {

        log.info("编辑请求进入");

        try {
            addressService.update(address);

            return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            log.error("编辑失败，失败原因：{}",e);
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    /**
     * 根据id调整状态
     *
     * @param id
     * @return
     */
    @PutMapping("{id}")
    public Result changeEnable(@PathVariable("id") Integer id){

        log.info("changeEnable请求进入");

        try {
            addressService.changeEnable(id);

            return new Result(true,MessageConstant.SWITCH_CHANGEEABLE_SUCCESS);
        } catch (Exception e) {
            log.error("编辑状态失败，失败原因：{}",e);
            return new Result(false,MessageConstant.SWITCH_CHANGEEABLE_FALL);
        }
    }


    /**
     * 根据Id删除Address数据
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteAddress(@PathVariable("id") Integer id){
        log.info("delete请求进入");
        try {
            addressService.delete(id);

            return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            log.error("删除失败，失败原因：{}",e);
            return new Result(false,MessageConstant.DELETE_ADDRESS_FALL);
        }

    }
}
